﻿var versionArray = [{ver:"4.0.0",wid:"com.tencent.mm:id/c9b"},{ver:"5.5.1",wid:"com.tencent.mm:id/c9b"},{ver:"7.0.1",wid:"com.tencent.mm:id/c9b"},{ver:"8.3.0",wid:"com.tencent.mm:id/c9b"},{ver:"9.0.0",wid:"com.tencent.mm:id/c9b"}];
var version = "";
var widgetLogin = "";
var packageName = "com.ss.android.ugc.aweme";
var startActivity = "com.ss.android.ugc.aweme.splash.SplashActivity";
function dyConditionCheck() {
	//检查是否
	var appName = PhoneEngine.getVersionName( "com.ss.android.ugc.aweme" );
    AccessibilityEngine.log("检查是否："+appName);
	if ( appName == "" ) {
		setTaskState( "6", "抖音未安装" );
		throw SyntaxError();
	} else {
        AccessibilityEngine.log("versionArray.length："+versionArray.length);
	   for ( var i = 0;i < versionArray.length;i++ ) {
	       if ( appName == versionArray[i].ver ) {
	           version = versionArray[i].ver;
	           widgetLogin = versionArray[i].wid;
	           AccessibilityEngine.log("抖音版本："+version);
	       }
       }
       if ( version == "" ) {
            setTaskState("6", "支持抖音4.0.0,5.5.1,7.0.1,8.3.0,9.0.0版本");
       		throw SyntaxError();
       }
	}
    // 判断辅助服务是否开启
    if ( AccessibilityEngine.isOpenService() == 0 ){
        setTaskState("6", "辅助服务未开启");
        throw SyntaxError();
    }

    // 判断屏幕引擎是否开启
    var runCode = ScreenEngine.isrun();
    var runCodeJson = JSON.parse(runCode);
    if (runCodeJson.code != 0) {
        setTaskState("6", "屏幕引擎未开启");
        throw SyntaxError();
    }

    //打开抖音
    AccessibilityEngine.mockLanuch( packageName, startActivity );
	AccessibilityEngine.sleepEngine( 2000 );
	if ( appName == "4.0.0" ) {
    	return "douyin400.js";
    }else if ( appName == "5.5.1" ) {
        return "douyin551.js";
    }else if ( appName == "7.0.1" ) {
        return "douyin701.js";
    }else if ( appName == "8.3.0" ) {
        return "douyin830.js";
    }else if ( appName == "8.3.0" ) {
        return "douyin900.js";
    }
}


function setTaskState(code, reason) {
    showLog(reason);
    var taskState = new Object();
    taskState.status = code;
    taskState.reason = reason;
    ResponseEngine.setTaskState(JSON.stringify(taskState));
}
function showLog(logText) {
    var time = getNowFormatDate();
    AccessibilityEngine.log(time+" "+logText);
}
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var currentdate = date.getHours() + seperator2 + date.getMinutes() +
        seperator2 + date.getSeconds();
    return currentdate;
}
//传入x值以720为基准
function parseX(x) {
    var screenWidth = PhoneEngine.getScreenWidth();
    var bili = screenWidth / 720;
    return Math.floor(bili * x);
}

//传入以y值以1280为基准
function parseY(y) {
    var screenHeight = PhoneEngine.getScreenHeight();
    var bili = screenHeight / 1280;
    return Math.floor(bili * y);
}
//获取XY
function getXYByIdIndex( idvar ) {
    var str = "";
    for (var s = 0; s < 30; s++) {
        str = AccessibilityEngine.getBoundsCenterIdIndex( idvar, 0 );
        if ( str != "") {
            break;
        }
        if (AccessibilityEngine.checkUpId(widget.pages.shouye.bugengxin) == ISTRUE) { //更新以后再说
            AccessibilityEngine.mockOnClickId(widget.pages.shouye.bugengxin);
        }
        if (AccessibilityEngine.checkUpId(widget.pages.shouye.wzdl) == ISTRUE) { //我知道了
            AccessibilityEngine.mockOnClickId(widget.pages.shouye.wzdl);
        }
        AccessibilityEngine.sleepEngine( 200 );
    }
    var wodeXY = JSON.parse( str );
    showLog("getXYByIdIndexX105:"+wodeXY.centerX);
    showLog("getXYByIdIndexY106:"+wodeXY.centerY);
    //if ( wodeXY.centerY == 1232 ) {551
	if ( wodeXY.centerY == 1221 ) {//701
        return 1;//4A
    } else if ( wodeXY.centerY == 1832 ) {
        return 2;//NOTE2
    } else if ( wodeXY.centerY == 1949 ) {
        return 3;//redmi 5 plus
    }
}

// function chuSiHua(bugengxin) {
//     var bugengxinF=0;
//     while(true){
//         if (AccessibilityEngine.checkUpId(bugengxin) == ISTRUE ){
//             break;
//         }else {
//             AccessibilityEngine.sleepEngine(1000);//等待
//             bugengxinF++;
//         }
//         if(bugengxinF>10){
//             break
//         }
//     }
// }